package com.zjxf.controller;
import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.CmsStory;
import com.zjxf.base.bean.ro.RoCmsStory;
import com.zjxf.base.bean.vo.VoCmsStory;
import com.zjxf.base.common.JSONResponse;
import com.zjxf.base.common.OperationException;
import com.zjxf.controller.common.BaseController;
import com.zjxf.service.CmsStoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 长征故事管理
 * Created by lhn on 2021/4/10.
 */
@Validated
@RestController
@AllArgsConstructor
@Api(tags = "LmStoryController", description = "长征故事")
@RequestMapping("/app/lmstory")
public class LmStoryController extends BaseController {

    private final CmsStoryService cmsStoryService;

    /**
     * 长征故事列表
     * @param filter
     * @return JSONResponse
     * @throws OperationException
     */
    @ApiOperation("长征故事列表")
    @PostMapping("findAllLmStory")
    public JSONResponse findLmStory(@Valid VoCmsStory.filter filter) {
        Page<CmsStory> cmsStoryPage = cmsStoryService.page(filter);
        JSONObject jsonObject = cmsStoryService.pageRo(cmsStoryPage);
        return success(jsonObject);
    }

    /**
     * 长征故事详情
     * @param id
     * @return
     * @throws OperationException 故事不存在
     */
    @ApiOperation("长征故事详情")
    @PostMapping (value = "findAllLmStory/{id}")
    public JSONResponse findLmStoryById(@NotNull @PathVariable Long id) throws OperationException {
        CmsStory cmsStory = cmsStoryService.getEntity(id);
        RoCmsStory.Details details = cmsStoryService.getEntityRo(cmsStory);
        return success(details);
    }
}

